<?php

if (!defined('IN_AJAX')) die(basename(__FILE__));

global $userdata, $lang, $ts_cfg;

$mode = (string) $this->request['mode'];

switch ($mode)
{
	case 'clear_cache':
	
		foreach ($ts_cfg['cache']['engines'] as $cache_name => $cache_val)
		{
			if (!in_array('db_sqlite', $cache_val))
			{
				CACHE($cache_name)->rm();
			}
		}
		$this->response['cache_html'] = '<span class="label label-success">'. $lang['ALL_CACHE_CLEARED'] .'</span>';
		break;
		
	case 'clear_datastore':
	
		global $datastore;
		
		$datastore->clean();
		
		$this->response['datastore_html'] = '<span class="label label-success">'. $lang['DATASTORE_CLEARED'] .'</span>';
		break;
		
	case 'clear_template_cache':
	
		global $template;
		
		$match = 'tmpl_';
		$match_len = strlen($match);
		$dir = $template->cachedir;
		$res = @opendir($dir);
		while (($file = readdir($res)) !== false)
		{
			if (substr($file, 0, $match_len) === $match)
			{
				@unlink($dir . $file);
			}
		}
		closedir($res);
		
		$this->response['template_cache_html'] = '<span class="label label-success">'. $lang['ALL_TEMPLATE_CLEARED'] .'</span>';
		break;
}
$this->response['mode'] = $mode;